package com.iwith.dongmoon.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.GridView;

import com.iwith.dongmoon.gsonlist.BoardList;
import com.iwith.dongmoon.gsonlist.BoardListItem;
import com.iwith.dongmoon.listadpater.IconGridAdapter;
import com.iwith.dongmoon.util.ApiCode;
import com.iwith.dongmoon.util.RequestGson;
import com.iwith.dongmoon.util.ResultCode;
import com.iwith.dongmoon.util.ThreadInterface;
import com.iwith.dongmoon.util.ThreadTask;

public class Tab_GroupActivity extends Activity {
	private GridView groupGridView;
	private IconGridAdapter groupAdapter;
	private Context mContext;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_group);
		// TODO Auto-generated method stub
	}

	@Override
	public void onResume(){
		super.onResume();
		ThreadTask.onFinishAll();
		Initialize();
	}
	
	@Override
	public void onPause(){
		super.onPause();
		ThreadTask.onFinishAll();
	}
	
	private void Initialize() {
		mContext = this;
		groupGridView = (GridView) findViewById(R.id.tabgroup_gridview);
		setData();
	}

	private void setData() {
		ThreadInterface threadInterface = new ThreadInterface() {
			BoardList boardList;
			RequestGson requestGson = new RequestGson();

			public void onThread() {
				// TODO Auto-generated method stub
				String api = ApiCode.API_GROUPCATEGORY;
				boardList = requestGson.getBoardGsonBySendingAPI(mContext, api,	"GET", "");
			}

			public void onThreadEnd() {
				// TODO Auto-generated method stub
				if (boardList.getResultCode().equals(ResultCode.SUCCESS) == true) {
					groupAdapter = new IconGridAdapter(Tab_GroupActivity.this,	R.layout.griditem_icon,	(ArrayList<BoardListItem>) boardList.getList());
					groupGridView.setAdapter(groupAdapter);
				} else {
					requestGson.alertToast(mContext, "데이터 로딩 실패");
				}
			}
		};

		ThreadTask task = new ThreadTask((Activity)mContext, threadInterface);
		task.execute();
	}
}
